package com.rs.startup;

import cn.hutool.core.util.RandomUtil;
import com.rs.cache.LocalCache;
import com.rs.entity.basic.DeviceStatus;
import com.rs.entity.basic.MapDataPointVO;
import com.rs.entity.enums.VehicleStatus;
import com.rs.push.DevicePushMessage;
import com.rs.service.map.IMapDataService;
import com.rs.util.NodeUtil;
import com.rs.util.VehicleUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 初始化
 *
 * @author zhouwz
 * @date 2020/9/14 14:47
 */
@Component
public class Init {

    /**
     * 索引记录
     */
    private static List<Integer> indexRecord;

    @Value("${init.mapNo}")
    private String mapNo;

    @Value("${init.vehicleNumber}")
    private Integer vehicleNumber;

    @Value("${init.loadVehicle}")
    private Boolean loadVehicle;

    private final IMapDataService iMapDataService;

    public Init(IMapDataService iMapDataService) {
        this.iMapDataService = iMapDataService;
    }

    @EventListener(value = WebServerInitializedEvent.class)
    public void start() {
        // 地图信息加载
        LocalCache.mapInfo = iMapDataService.getMapData(mapNo);
        // 车辆信息加载
        if (loadVehicle) {
            loadVehicleList();
        }
    }

    /**
     * 车辆信息加载
     */
    private void loadVehicleList() {
        for (int i = 1; i <= vehicleNumber; i++) {
            MapDataPointVO point = LocalCache.mapInfo.getMapDataPointList().get(getNotConflictIndex());
            String vehicleCode = String.valueOf(i);
            DeviceStatus deviceStatus = new DeviceStatus()
                    .setDeviceNo(vehicleCode)
                    .setStatus(VehicleStatus.IDLE.getCode())
                    .setXCoordinate(point.getXCoordinate())
                    .setYCoordinate(point.getYCoordinate())
                    .setPower(new BigDecimal(RandomUtil.randomInt(60, 100)))
                    .setAngle(new BigDecimal(RandomUtil.randomInt(-180, 180)))
                    .setNodeName(point.getNodeName())
                    .setNodeCode(point.getNodeCode());
            // 初始化加载设备状态
            VehicleUtil.putCurrent(deviceStatus);
            DevicePushMessage.reportStatus2Rcs(vehicleCode);
            DevicePushMessage.nodeReport(vehicleCode, false);
            LocalCache.deviceMap.put(vehicleCode, deviceStatus);
        }
    }

    /**
     * 获取不冲突的点位索引
     *
     * @return 索引
     */
    public int getNotConflictIndex() {
        int index = RandomUtil.randomInt(0, NodeUtil.getNodeSize());
        if (!getIndexRecord().contains(index)) {
            getIndexRecord().add(index);
            return index;
        } else {
            return getNotConflictIndex();
        }
    }

    public static List<Integer> getIndexRecord() {
        if (indexRecord == null) {
            indexRecord = new ArrayList<>();
            return indexRecord;
        }
        return indexRecord;
    }
}